# -*- coding: utf-8 -*-
# @Time: 2025/5/25 13:50
# @Author: wzd
# @Email: 2146333089@qq.com
# @File: serializers.py
# courses/serializers.py
from rest_framework import serializers
from .models import Course


class CourseUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Course
        fields = [
            'title', 'description', 'learning_objectives',
            'requirements', 'duration', 'status', 'published_at'
        ]

    def validate_status(self, value):
        valid_statuses = ['draft', 'published', 'archived']
        if value not in valid_statuses:
            raise serializers.ValidationError("无效的课程状态")
        return value

    def validate_published_at(self, value):
        if self.initial_data.get('status') == 'published' and not value:
            raise serializers.ValidationError("已发布课程必须设置发布时间")
        return value